
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class TextDB {
	public static final String SEPARATOR = "|";

    // an example of reading
	public static ArrayList readMovies(String filename) throws IOException {
		// read String from text file
		ArrayList stringArray = (ArrayList)read(filename);
		ArrayList alr = new ArrayList() ;// to store Professors data

        for (int i = 0 ; i < stringArray.size() ; i++) {
				String st = (String)stringArray.get(i);
				// get individual 'fields' of the string separated by SEPARATOR
				StringTokenizer star = new StringTokenizer(st , SEPARATOR);	// pass in the string to the string tokenizer using delimiter ","

				int movieid = Integer.parseInt(star.nextToken().trim());
				String moviename = star.nextToken().trim();
				String moviedate = star.nextToken().trim();
				String movietype = star.nextToken().trim();
				String movierating = star.nextToken().trim();
				String moviestatus = star.nextToken().trim();
				String movieduration = star.nextToken().trim();
				int  movieprice = Integer.parseInt(star.nextToken().trim()); // third token
				// create Movie object from file data
				Movie movie = new Movie(movieid,moviename,moviedate,movietype,movierating,moviestatus,movieduration,movieprice);
				// add to Movie list
				alr.add(movie) ;
			}
			return alr ;
	}

  // an example of saving
public static void saveMovies(String filename, List al) throws IOException {
		List alw = new ArrayList() ;// to store Professors data

        for (int i = 0 ; i < al.size() ; i++) {
				Movie movie = (Movie)al.get(i);
				StringBuilder st =  new StringBuilder() ;
				st.append(movie.getID());
				st.append(SEPARATOR);
				st.append(movie.getName().trim());
				st.append(SEPARATOR);
				st.append(movie.getDate().trim());
				st.append(SEPARATOR);
				st.append(movie.getType().trim());
				st.append(SEPARATOR);
				st.append(movie.getRating().trim());
				st.append(SEPARATOR);
				st.append(movie.getStatus().trim());
				st.append(SEPARATOR);
				st.append(movie.getDuration().trim());
				st.append(SEPARATOR);
				st.append(movie.getPrice());
				alw.add(st.toString()) ;
			}
			write(filename,alw);
	}

  /** Write fixed content to the given file. */
  public static void write(String fileName, List data) throws IOException  {
    PrintWriter out = new PrintWriter(new FileWriter(fileName));

    try {
		for (int i =0; i < data.size() ; i++) {
      		out.println((String)data.get(i));
		}
    }
    finally {
      out.close();
    }
  }

  /** Read the contents of the given file. */
  public static List read(String fileName) throws IOException {
	List data = new ArrayList() ;
    Scanner scanner = new Scanner(new FileInputStream(fileName));
    try {
      while (scanner.hasNextLine()){
        data.add(scanner.nextLine());
      }
    }
    finally{
      scanner.close();
    }
    return data;
  }

public static void main(String[] aArgs)  {
    	
    	String filename = "Movies_data.txt" ;
		try {
			// read file containing Professor records.
			ArrayList al = TextDB.readMovies(filename) ;
			for (int i = 0 ; i < al.size() ; i++) {
					System.out.println();
					Movie movie = (Movie)al.get(i);
					System.out.print("ID:" + movie.getID()+" ");
					System.out.print("Title:" + movie.getName()+" ");
					System.out.print("Date:" + movie.getDate()+" ");
					System.out.print("Type:" + movie.getType()+" ");
					System.out.print("Rating:" + movie.getRating()+" ");
					System.out.print("Status:" + movie.getStatus()+" ");
					System.out.print("Duration:" + movie.getDuration()+" ");
					System.out.print("Price:" + movie.getPrice()+" ");
					
			}
			Movie m1 = new Movie(2,"aa","2013-10-27","thriller","10","Showing","1h2mins",10);
			// al is an array list containing Professor objs
			al.add(m1);
			// write Professor record/s to file.
			TextDB.saveMovies(filename, al);
		}catch (IOException e) {
			System.out.println("IOException > " + e.getMessage());
		}
  }
}
